package com.zisuyi.uilibrary.base

import com.weilele.mvvm.base.helper.*
import com.zisuyi.game.sdk.net.GameModel
import com.zisuyi.game.sdk.net.bean.SmallGameInfoBean
import com.zisuyi.game.sdk.net.bean.SmallGameRewardBean
import com.zisuyi.game.sdk.net.bean.req.GameRewardReq

class SmallGameViewModel : BaseUiViewModel() {
    /**
     * 获取游戏信息
     */
    val gameInfoData by lazy {
        createStatusMediatorLiveData<SmallGameInfoBean>().apply {
            addSource(gameRewardData) {
                when (it) {
                    is StatusValue.Error -> {
                        error(it.nonNullMsg)
                    }
                    is StatusValue.Running -> {
                        running()
                    }
                    is StatusValue.Success -> {
                        success(SmallGameInfoBean().apply {
                            info = it.nonNullValue.info.info
                        })
                    }
                }
            }
        }
    }

    private val gameRewardData by lazy { createStatusLiveData<SmallGameRewardBean>() }

    /**
     * 加载游戏信息
     */
    fun loadGameInfo(gameName: String) {
        gameInfoData.retrofitGame {
            GameModel.getSmallGameInfo(gameName)
        }
    }

    /**
     * 获取游戏奖励
     */
    fun getReward(gameName: String?, isIsMul: Boolean) {
        gameName ?: return
        gameRewardData.retrofitGameReward {
            val bean = GameRewardReq()
            val reward = GameRewardReq.DataDTO()
            reward.name = gameName
            reward.type = "REWARD"
            reward.isIsMul = isIsMul
            bean.data = reward
            GameModel.gameReward(gameName, bean)
        }
    }

    /**
     * 看视频增加次数
     */
    fun addGameTimesByVideoAd(gameName: String?) {
        gameName ?: return
        gameRewardData.retrofitGameReward {
            val bean = GameRewardReq()
            val reward = GameRewardReq.DataDTO()
            reward.name = gameName
            reward.type = "REDEEM"
            bean.data = reward
            GameModel.gameReward(gameName, bean)
        }
    }
}